const app = getApp()
import {
  imageUrl
} from "../../utils/imageutil.js"
Page({
  data: {
    imageUrl: imageUrl,
    navcurr: 1,
    keys: '',
    page: 1,
    flag: true,
    type: '',
    navlist: [{
      title: '全部',
      id: 1
    }, {
      title: '待付款',
      id: 2
    }, {
      title: '待接单',
      id: 3
    }, {
      title: '待服务',
      id: 4
    }, {
      title: '待评价',
      id: 5
    }, {
      title: '已完成',
      id: 6
    }],
    list: [],

    paytype_list: [],
    has_jump: false,
    show_code_img: false,
    is_bank: false,

  },
  changernavcurr(e) { //订单顶部状态切换
    this.setData({
      navcurr: e.currentTarget.dataset.id,
      type: e.currentTarget.dataset.id - 1,
      flag: true,
      page: 1,
      keys: '',
      list: []
    })
    this.getList()
  },
  onLoad: function (options) {
    if (options.navcurr) {
      this.setData({
        navcurr: options.navcurr,
        type: Number(options.navcurr) - 1
      })
    }
    this.getList()
  },

  onShow() {
    if (this.data.has_jump) {
      if (this.data.is_bank) {
        this.setData({
          is_bank: false
        })
        getApp().showModal('是否已完成支付', true).then(() => {
          this.reset_list()
        })
      } else {
        this.reset_list()
      }
    }
  },

  reset_list() {
    this.setData({
      navcurr: 1,
      list: [],
    })
    this.getList()
  },


  get_paytype(storeid) {
    return new Promise((a, b) => {
      getApp().globalData.post({
        url: '/api/applet.ouyoung/paytype.html',
        data: {
          storeid
        }
      }, true).then(res => {
        this.setData({
          paytype_list: res.data.data
        })
        a()
      })
    })
  },

  onReachBottom() {
    this.getList()
  },
  to_Cancel(e) { //取消订单
    wx.navigateTo({
      url: '../matron/abrogate?wid=' + e.currentTarget.dataset.id + '&type=' + 2,
    })
  },
  pay(e) { //微信支付
    let store_id = e.currentTarget.dataset.store_id
    this.get_paytype(store_id).then(() => {
      let paytype_list = this.data.paytype_list
      let itemList = []
      paytype_list.forEach((v, i) => {
        itemList.push(v.title)
      })
      wx.showActionSheet({
        itemList,
        success: (r) => {
          let tapIndex = r.tapIndex
          let paytype = paytype_list[tapIndex].paytype

          let _this = this;
          if (paytype == 6) {
            wx.setStorageSync('pay_info', {
              do: 'pay',
              paytype,
              order_id: e.currentTarget.dataset.id,
              user_id: wx.getStorageSync('userInfo').id,
              openid: wx.getStorageSync('userInfo').openid,
              money: e.currentTarget.dataset.money,
            })
            wx.navigateTo({
              url: '/online_bank/list?id=' + e.currentTarget.dataset.id
            })
            return
          }
          app.globalData.post({
            data: {
              do: 'pay',
              paytype,
              order_id: e.currentTarget.dataset.id,
              user_id: wx.getStorageSync('userInfo').id,
              openid: wx.getStorageSync('userInfo').openid,
              money: e.currentTarget.dataset.money,
            }
          }).then(res => {
            let data = res.data
            if (data.pay_status == 2) {
              wx.showModal({
                title: '提示',
                content: '支付成功',
                showCancel: false,
                success: () => {
                  _this.setData({
                    navcurr: 1,
                    list: [],
                  })
                  _this.getList()
                }
              })
              return
            }
            if (getApp().globalData.appletpayves_info.work == 3) {
              getApp().payPlugin(data.orderNo).then(() => {
                getApp().showModal('支付成功').then(() => {
                  _this.setData({
                    navcurr: 1,
                    list: [],
                  })
                  _this.getList()
                })
              })
              return
            }
            if (data.jump) {
              this.selectComponent('#zfb_pay').get_code(data.jump)
              this.setData({
                show_code_img: true,
                has_jump: true,
              })
              return
            } else {
              this.setData({
                has_jump: false
              })
            }
            wx.requestPayment({
              nonceStr: data.nonceStr,
              package: data.package,
              paySign: data.paySign,
              timeStamp: data.timeStamp,
              signType: data.signType,
              complete(s) {
                if ("requestPayment:ok" == s.errMsg) {
                  wx.showToast({
                    title: "支付成功"
                  })
                  _this.setData({
                    navcurr: 1,
                    list: [],
                  })
                  _this.getList()
                }
              }
            })
          })

        }
      })
    })

  },
  getList() { //订单列表
    if (!this.data.flag) {
      return
    }
    this.setData({
      flag: false
    })
    app.globalData.post({
      data: {
        do: 'MyOrder',
        type: this.data.type,
        page: this.data.page,
        pagesize: 10,
        key: this.data.keys,
        user_id: wx.getStorageSync('userInfo').id
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      if (res.data.data.length == 0) {
        this.setData({
          flag: false
        })
      } else {
        let arr = this.data.list.concat(res.data.data);
        this.setData({
          list: arr,
          flag: true,
          page: this.data.page + 1
        })
      }
    })
  },
  change_keys(e) {
    this.setData({
      keys: e.detail.value,
      page: 1,
      flag: true,
      list: []
    })
    this.getList()
  },
  to_plan(e) { //订单详情
    wx.navigateTo({
      url: '../matron/plan?order_id=' + e.currentTarget.dataset.id,
    })
  },
  applyRefundTcOrder(e) {
    if (e.currentTarget.dataset.refund > 0) {
      return
    }
    getApp().globalData.posts('/api/ouyoung/order/applyRefundTcOrder', {
      order_number: e.currentTarget.dataset.order,
    }).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        this.setData({
          page: 1,
          list: []
        })
        this.getList()
      })
    })
  },
  appraise(e) { //评价
    wx.navigateTo({
      url: './appraise?id=' + e.currentTarget.dataset.id,
    })
  },
})